﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

using Factonomy.AzureUtility.Core;
using Factonomy.AzureUtility.Core.Storage;
using Factonomy.AzureUtility.Core.StorageHelpers;

namespace Factonomy.AzureUtility.Dialogs
{
    /// <summary>
    /// Interaction logic for TextEditor.xaml
    /// </summary>
    public partial class TextEditor : Window
    {
        private BlobProperties _properties = null;
        private BlobContainer _container;
        private bool _textChanged = false;

        public TextEditor(BlobProperties properties, BlobContainer container)
        {
            InitializeComponent();

            
            
            this._properties = properties;
            this._container = container;
            this.uiBlobName.Text = properties.Name;

            ConfigAccount account = Config.Instance.GetAccount(this._container.AccountName);
            MemoryStream stream = BlobHelper.Instance.GetBlobContents(account, this._container.ContainerName, this._properties.Name);

            TextRange range = new TextRange(this.uiTextboxMain.Document.ContentStart, this.uiTextboxMain.Document.ContentEnd);
            range.Load(stream, System.Windows.DataFormats.Text);

            stream.Close();


            this.uiTextboxMain.TextChanged += new TextChangedEventHandler(uiTextboxMain_TextChanged);
        }

        void uiTextboxMain_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (!this._textChanged)
            {
                this.Title = this.Title + "*";
                this._textChanged = true;
            }
        }

        private void uiMenuItemSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                this.SaveBlob();
                this.Title = this.Title.Substring(0, this.Title.Length - 1);
                this._textChanged = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
            }
        }

        private void PromptForSave()
        {
            if (this._textChanged)
            {
                if (MessageBox.Show("Do you wish to save your changes?", "Confirm Save", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
                {
                    this.SaveBlob();
                }
            }

        }

        private void SaveBlob()
        {
            ConfigAccount account = Config.Instance.GetAccount(this._container.AccountName);
            TextRange textRange = new TextRange(this.uiTextboxMain.Document.ContentStart, this.uiTextboxMain.Document.ContentEnd);
            BlobHelper.Instance.SaveBlob(account, this._container.ContainerName, this._properties.Name, textRange.Text);
        }

        private void uiMenuItemClose_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
        {
            this.PromptForSave();
            base.OnClosing(e);
        }
    }
}
